﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class UCSyncPhonebook : UserControl
    {
        public delegate void UCSyncPhonebookEventHandler(object sender, UCSyncPhonebookEventArgs e);
        public event UCSyncPhonebookEventHandler EventUCSyncPhonebook;
        private bool IsLoad = false;

        public UCSyncPhonebook()
        {
            InitializeComponent();
        }
        public int Index { get; set; }

        public string NameContact
        {
            get { return this.TxtName.Text; }
            set
            {
                value = value.Trim();
                if (value.Length > TxtName.MaxLength)
                {
                    value = value.Substring(0, TxtName.MaxLength);
                }
                this.TxtName.Tag = value;
                this.TxtName.Text = value;
            }
        }

        public void ChangeNameContact(string NewName)
        {
            this.TxtName.Text = NewName;
        }

        public bool IsHasChange()
        {
            bool ret = false;
            if (ChkSendP.Checked != Boolean.Parse(ChkSendP.Tag.ToString()))
            {
                ret = true;
            }
            if (ChkSendS.Checked != Boolean.Parse(ChkSendS.Tag.ToString()))
            {
                ret = true;
            }
            if (TxtName.Text != TxtName.Tag.ToString())
            {
                ret = true;
            }
            if (TxtNumber.Text != TxtNumber.Tag.ToString())
            {
                ret = true;
            }
            return ret;
        }

        public string NameLiveGoogleContact
        {
            get { return this.TxtLiveGoogleName.Text; }
            set
            {
                value = value.Trim();
                if (value.Length > TxtLiveGoogleName.MaxLength)
                {
                    value = value.Substring(0, TxtLiveGoogleName.MaxLength);
                }
                this.TxtLiveGoogleName.Tag = value;
                this.TxtLiveGoogleName.Text = value;
            }
        }

        public string NumberContact
        {
            get { return this.TxtNumber.Text; }
            set
            {
                value = value.Trim();
                if (value.Length > TxtNumber.MaxLength)
                {
                    value = value.Substring(0, TxtNumber.MaxLength);
                }
                this.TxtNumber.Tag = value;
                this.TxtNumber.Text = value;
            }
        }

        public string NumberLiveGoogleContact
        {
            get { return this.TxtLiveGoogleNumber.Text; }
            set
            {
                value = value.Trim();
                if (value.Length > TxtLiveGoogleNumber.MaxLength)
                {
                    value = value.Substring(0, TxtLiveGoogleNumber.MaxLength);
                }
                this.TxtLiveGoogleNumber.Tag = value;
                this.TxtLiveGoogleNumber.Text = value;
            }
        }

        public bool IsRemoveEnabled
        {
            get { return this.ChkDel.Enabled; }
            set { this.ChkDel.Enabled = value; }
        }

        public bool SendPhoneEnabled
        {
            get { return this.ChkSendP.Enabled; }
            set { this.ChkSendP.Enabled = value; }
        }

        public bool SendSIMEnabled
        {
            get { return this.ChkSendS.Enabled; }
            set { this.ChkSendS.Enabled = value; }
        }

        public bool SendPhone
        {
            get { return this.ChkSendP.Checked; }
            set
            {
                this.ChkSendP.Tag = value.ToString();
                this.ChkSendP.Checked = value;
                if (value)
                {
                    this.ChkDel.Enabled = false;
                }
                else
                {
                    if (SendSIM)
                    {
                        this.ChkDel.Enabled = false;
                    }
                    else
                    {
                        this.ChkDel.Enabled = true;
                    }
                }
                if (value)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                }
                else
                {
                    if (ChkSendS.Checked)
                    {
                        TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                        TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                    }
                    else
                    {
                        TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                        TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                    }
                }
                if (TxtName.Text.Length > TxtName.MaxLength)
                {
                    TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
                }
                if (TxtNumber.Text.Length > TxtNumber.MaxLength)
                {
                    TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
                }
            }
        }

        public bool SendSIM
        {
            get { return this.ChkSendS.Checked; }
            set
            {
                this.ChkSendS.Tag = value.ToString();
                this.ChkSendS.Checked = value;
                if (value)
                {
                    this.ChkDel.Enabled = false;
                    ChkSendS.Enabled = false;
                }
                else
                {
                    if (SendPhone)
                    {
                        this.ChkDel.Enabled = false;
                    }
                    else
                    {
                        this.ChkDel.Enabled = true;
                    }
                }
                if (value)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                }
                else
                {
                    if (ChkSendP.Checked)
                    {
                        TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                        TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                    }
                    else
                    {
                        TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                        TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                    }
                }
                if (TxtName.Text.Length > TxtName.MaxLength)
                {
                    TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
                }
                if (TxtNumber.Text.Length > TxtNumber.MaxLength)
                {
                    TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
                }
            }
        }

        public bool IsRemove
        {
            get { return ChkDel.Checked; }
        }

        private bool _RedColor = false;
        public bool RedColor
        {
            get { return _RedColor; }
            set
            {
                _RedColor = value;
                if (value)
                {
                    TxtName.ForeColor = Color.Red;
                    TxtNumber.ForeColor = Color.Red;
                }
                else
                {
                    TxtName.ForeColor = SystemColors.WindowText;
                    TxtNumber.ForeColor = SystemColors.WindowText;
                }
            }
        }

        private BookSyncEnum _From = BookSyncEnum.None;
        public BookSyncEnum From 
        {
            get {return _From ;} 
            set
            {
                _From = value;
                switch (_From)
                {
                    case BookSyncEnum.None:
                    case BookSyncEnum.SIM:
                    case BookSyncEnum.Phone:
                        PctFrom.Image = null;
                        break;
                    case BookSyncEnum.Live:
                        PctFrom.Image = Properties.Resources.live;
                        break;
                    case BookSyncEnum.Goolgle:
                        PctFrom.Image = Properties.Resources.GOOGLE;
                        break;
                }
            } 
        }
        public string NumberType { get; set; }
        public string LiveGoogleID { get; set; }
        public string PhoneID { get; set; }
        public string SIMID { get; set; }

        private bool _IsSync = true;
        public bool IsSync 
        {
            get { return _IsSync;}
            set
            {
                _IsSync = value;
                if (_IsSync)
                {
                    PctSync.Image = Properties.Resources.On;
                    LblSync.Text = ResourcesText.AliasSync;
                }
                else
                {
                    PctSync.Image = Properties.Resources.Off;
                    LblSync.Text = ResourcesText.AliasNotsync;
                }
            }
        }

        private void BntCopy_Click(object sender, EventArgs e)
        {
            TxtName.Text = TxtLiveGoogleName.Text.Trim();
            TxtNumber.Text = TxtLiveGoogleNumber.Text.ToUpper().Replace(" ", "").Replace("-", "").Replace("X", "").Replace("+","").Trim();
            if (PhoneID != "")
            {
                ChkSendP.Enabled = false;
            }
            else 
            {
                ChkSendP.Enabled = true;
            }
            if (SIMID != "")
            {
                ChkSendS.Enabled = false;
            }
            else
            {
                ChkSendS.Enabled = true;
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
        }
        public bool IsValidateIndexSync(string IndexP, string IndexS)
        {
            bool ret = true;
            if (IndexP != "")
            {
                PhoneBook pbm = SharedLib.FindPhoneByID(IndexP, false);
                if (pbm == null)
                {
                    this.PhoneID = "";
                    this.SendPhone = false;
                    this.IsSync = false;
                    ret = false;
                }
                else
                {
                    if (this.NameContact != pbm.Name || this.NumberContact != pbm.Number)
                    {
                        this.TxtName.Text = pbm.Name.Trim();
                        this.TxtName.Tag = "";
                        this.TxtNumber.Text = pbm.Number.Trim();
                        this.TxtNumber.Tag = "";
                        this.IsSync = false;
                        this.RedColor = true;
                        ret = false;
                    }
                }
            }
            if (IndexP != "")
            {
                PhoneBook pbm = SharedLib.FindPhoneByID(IndexP, false);
                if (pbm == null)
                {
                    this.SIMID = "";
                    this.SendSIM = false;
                    this.IsSync = false;
                    ret = false;
                }
                else
                {
                    if (this.NameContact != pbm.Name || this.NumberContact != pbm.Number)
                    {
                        this.TxtName.Text = pbm.Name.Trim() ;
                        this.TxtName.Tag = "";
                        this.TxtNumber.Text = pbm.Number.Trim();
                        this.TxtNumber.Tag = "";
                        this.IsSync = false;
                        this.RedColor = true;
                        ret = false;
                    }
                }
            }
            if (PhoneID != "")
            {
                ChkSendP.Enabled = false;
            }
            else
            {
                if (TxtName.Text.Trim() != "" && TxtNumber.Text.Trim() != "")
                {
                    ChkSendP.Enabled = true;
                }
                else
                {
                    ChkSendP.Enabled = false;
                }
            }
            if (SIMID != "")
            {
                ChkSendS.Enabled = false;
            }
            else
            {
                if (TxtName.Text.Trim() != "" && TxtNumber.Text.Trim() != "")
                {
                    ChkSendS.Enabled = true;
                }
                else
                {
                    ChkSendS.Enabled = false;
                }
            }
            if (SIMID == "" && PhoneID == "" && this.IsSync)
            {
                errorProviderName.SetError(TxtLiveGoogleName, ResourcesText.SyncNotPb);
            }
            else
            {
                errorProviderName.SetError(TxtLiveGoogleName, "");
            }
            return ret;
        }

        private void ChkSendP_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkSendP.Checked || ChkSendS.Checked)
            {
                ChkDel.Checked = false;
                ChkDel.Enabled = false;
            }
            else
            {
                ChkDel.Enabled = true;
            }
            if (ChkSendP.Checked)
            {
                TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
            }
            else
            {
                if (ChkSendS.Checked)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                }
                else
                {
                    TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                    TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                }
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
            if (EventUCSyncPhonebook != null && IsLoad)
            {
                EventUCSyncPhonebook(this, new UCSyncPhonebookEventArgs(ChkSendP.Checked ? 1 : -1,0));
            }
        }

        private void ChkSendS_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkSendP.Checked || ChkSendS.Checked)
            {
                ChkDel.Checked = false;
                ChkDel.Enabled = false;
            }
            else
            {
                ChkDel.Enabled = true;
            }
            if (ChkSendS.Checked)
            {
                TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
            }
            else
            {
                if (ChkSendP.Checked)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                }
                else
                {
                    TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                    TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                }
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
            if (EventUCSyncPhonebook != null && IsLoad)
            {
                EventUCSyncPhonebook(this, new UCSyncPhonebookEventArgs(0,ChkSendS.Checked?1: -1));
            }
        }


        private void TxtName_TextChanged(object sender, EventArgs e)
        {
            if (TxtName.Text == "")
            {
                if (PhoneID == "")
                {
                    ChkSendP.Checked = false;
                }
                if (SIMID == "")
                {
                    ChkSendS.Checked = false;
                }
                ChkSendS.Enabled = false;
                ChkSendP.Enabled = false;
                TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                return;
            }
            else
            {
                if (PhoneID != "")
                {
                    ChkSendP.Enabled = false;
                }
                else
                {
                    ChkSendP.Enabled = true;
                }
                if (SIMID != "")
                {
                    ChkSendS.Enabled = false;
                }
                else
                {
                    ChkSendS.Enabled = true;
                }
            }
        }

        private void TxtNumber_TextChanged(object sender, EventArgs e)
        {
            if (TxtNumber.Text == "")
            {
                if (PhoneID == "")
                {
                    ChkSendP.Checked = false;
                }
                if (SIMID == "")
                {
                    ChkSendS.Checked = false;
                }
                ChkSendS.Enabled = false;
                ChkSendP.Enabled = false;
                TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                return;
            }
            else
            {
                if (PhoneID != "")
                {
                    ChkSendP.Enabled = false;
                }
                else
                {
                    ChkSendP.Enabled = true;
                }
                if (SIMID != "")
                {
                    ChkSendS.Enabled = false;
                }
                else
                {
                    ChkSendS.Enabled = true;
                }
            }
        }

        private void TxtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void TxtNumber_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                TxtNumber.Text = TxtNumber.Tag.ToString();
                if (TxtNumber.Text.Length > TxtNumber.MaxLength)
                {
                    TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
                }
            }
        }

        private void TxtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                TxtName.Text = TxtName.Tag.ToString();
                if (TxtName.Text.Length > TxtName.MaxLength)
                {
                    TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
                }
            }
        }

        private void UCSyncPhonebook_Load(object sender, EventArgs e)
        {
            IsValidateIndexSync(PhoneID,SIMID);
            TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
            TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
            if (ChkSendP.Checked)
            {
                TxtName.MaxLength = Math.Min(TxtName.MaxLength, SharedLib.PhoneBookMEMNameMaxLength);
                TxtNumber.MaxLength = Math.Min(TxtNumber.MaxLength, SharedLib.PhoneBookMEMNameMaxLength);
            }
            if (ChkSendS.Checked)
            {
                TxtName.MaxLength = Math.Min(TxtName.MaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Min(TxtNumber.MaxLength, SharedLib.PhoneBookSIMNameMaxLength); 
            }
            IsLoad = true;
        }
    }
    public class UCSyncPhonebookEventArgs : EventArgs
    {
        private int _SendPhone;
        private int _SendSim;
        public UCSyncPhonebookEventArgs(int SendPhone, int SendSim)
        {
            _SendPhone = SendPhone;
            _SendSim = SendSim;
        }
        public int SendPhone { get { return _SendPhone; } }
        public int SendSIM { get { return _SendSim; } }
    }
}
